/*
* This file is a part of the open-eBackup project.
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright (c) [2024] Huawei Technologies Co.,Ltd.
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*/
#include "common/utils/Utils.h"
#include "protect_engines/hcs/common/HcsMacros.h"
#include "common/JsonUtils.h"
#include "HcsOpServiceUtils.h"

HCS_PLUGIN_NAMESPACE_BEGIN

HcsOpServiceUtils::HcsOpServiceUtils()
{}

HcsOpServiceUtils::~HcsOpServiceUtils()
{}

void HcsOpServiceUtils::GetAppEnv(const ApplicationEnvironment &appenv)
{
    m_appEnv = appenv;
    GetOpServiceInfo();
}

bool HcsOpServiceUtils::GetOpServiceInfo()
{
    INFOLOG("Enter to get opserviceinfo.");
    m_isOpServiceEnv = false;
    Json::Reader appEnvExtendInfoReader;
    Json::Value appEnvExtendInfoJson;
    if (!appEnvExtendInfoReader.parse(m_appEnv.extendInfo, appEnvExtendInfoJson)) {
        ERRLOG("Failed to read extendInfo.");
        return false;
    }
    std::string extendInfoString = appEnvExtendInfoJson.toStyledString();
    if (!Module::JsonHelper::JsonStringToStruct(extendInfoString, m_hcsOpServiceGetInfo)) {
        ERRLOG("Failed to parse extendInfo.");
    }
    if (m_hcsOpServiceGetInfo.m_tokenInfo != "" || m_hcsOpServiceGetInfo.m_isOpService == "1") {
        m_isOpServiceEnv = true;
        if (SetTokenInfo()) {
            INFOLOG("Get opserviceinfo success");
            return true;
        } else {
            ERRLOG("Failed to get token info");
            return false;
        }
    }

    INFOLOG("The scence is common scence");
    return true;
}

// 获得token和过期时间
bool HcsOpServiceUtils::SetTokenInfo()
{
    if (!Module::JsonHelper::JsonStringToStruct(m_hcsOpServiceGetInfo.m_tokenInfo, m_hcsOpServiceTokenInfo)) {
        ERRLOG("Failed to parse extendInfo.");
        return false;
    }
    return true;
}

std::string HcsOpServiceUtils::GetToken() const
{
    return m_hcsOpServiceTokenInfo.m_token.empty() ? m_hcsOpServiceTokenInfo.m_tokenStr : m_hcsOpServiceTokenInfo.m_token;
}

std::string HcsOpServiceUtils::GetExpireAtTime() const
{
    return m_hcsOpServiceTokenInfo.m_expiresAt;
}

std::string HcsOpServiceUtils::GetRegion() const
{
    return m_hcsOpServiceGetInfo.m_region_id;
}

std::string HcsOpServiceUtils::GetDomain() const
{
    return m_hcsOpServiceGetInfo.m_domain;
}

std::string HcsOpServiceUtils::GetProjectId() const
{
    return m_hcsOpServiceGetInfo.m_project_id;
}

bool HcsOpServiceUtils::GetIsOpServiceEnv() const
{
    return m_isOpServiceEnv;
}

// 更新token
void HcsOpServiceUtils::SetToken(const std::string &token)
{
    m_hcsOpServiceTokenInfo.m_token = token;
    m_hcsOpServiceTokenInfo.m_tokenStr = token;
}

HCS_PLUGIN_NAMESPACE_END